*FIRST, MERGE EXPENDITURE AND RENT DATASETS
*use rent8812_mike_murto , clear
*only keep observations on primary house (these are the observations that match the expenditure data) 
*keep if samp_un==1
*sort newid
*save rent8812_mike_murto_adj , replace 

*use inequality_data_for_mike_murto , clear 
*sort newid 
*merge 1:1 newid using rent8812_mike_murto_adj 
*save inequality_data_rent , replace 

*create predictions for utilities 
*do Utilities_predict 

*CREATE FILE FOR MANY TO ONE MERGE
use inequality_data_for_mike_murto , clear 

*correct 1970s measures
drop if (renteq==. & cutenure<=3 & (ref_year==1972 | ref_year==1973))

gen double hflow2 = 0
replace hflow2 = renteq*12 if (cutenure<=3 & (ref_year==1972 | ref_year==1973))

replace cons5 = ((cons5*scale)+(hflow2/4)-(hflow/4))/scale if (ref_year==1972 | ref_year==1973)

save inequality_data_70s_correct , replace 

***CREATE DEMOGRAPHIC VARIABLES FOR ELASTICITY REGRESSIONS
*create binary variable for gender
gen male=2-sex_ref
*Gen family type dummies
forvalues x=1/5 {
gen fam`x'=0
replace fam`x'=1 if ftype==`x'
gen fam_male`x'=fam`x'*male
}
*Create region dummies 
forvalues x=1/4 {
gen reg`x' =0
replace reg`x'=1 if region==`x'
replace reg`x'=0 if bls_urbn==2
}
gen urban=2-bls_urbn
*Create race dummies
gen race1=0
replace race1=1 if (ref_race==1 & hispanic==0)
gen race2=0
replace race2=1 if (ref_race==2 & hispanic==0)
gen race3=1-race1-race2
*Gen employment indicators 
gen emp=0 if (incweek1 != .)
replace emp=1 if (incweek1 !=. & incweek1>0)
gen emp2=0 if (incweek2 != .) 
replace emp2=1 if (incweek2 != . & incweek2>0)
gen emp_type=0
replace emp_type=1 if (emp==1 & emp2==1)
replace emp_type=2 if (emp==1 & emp2==0)
replace emp_type=3 if (emp==0 & emp2==1)
replace emp_type=4 if (emp==0 & emp2==0)
*Gen race indicator  
gen race_type=. 
replace race_type=1 if (race1==1)
replace race_type=2 if (race2==1)
replace race_type=3 if (race3==1)
*Gen education indicators and dummies (note, ed_type exists for earlier years, drop before creating new variable)
rename ed_type old_ed_type
gen ed_type=.
replace ed_type = old_ed_type if (ref_year<1970)
replace ed_type=1 if (ref_year>=1972 & ref_year<=1973 & (educ_ref==1 | educ_ref==2 | educ_ref==6))
replace ed_type=2 if (ref_year>=1972 & ref_year<=1973 & educ_ref==3)
replace ed_type=3 if (ref_year>=1972 & ref_year<=1973 & educ_ref==4)
replace ed_type=4 if (ref_year>=1972 & ref_year<=1973 & educ_ref==5)
replace ed_type=1 if (qyear>=801 & qyear<=955 & (educ_ref==1 | educ_ref==2 | educ_ref==7))
replace ed_type=2 if (qyear>=801 & qyear<=955 & educ_ref==3)
replace ed_type=3 if (qyear>=801 & qyear<=955 & educ_ref==4)
replace ed_type=4 if (qyear>=801 & qyear<=955 & (educ_ref==5 | educ_ref==6))
replace ed_type=1 if (qyear>955 & (educ_ref==0 | educ_ref==10 | educ_ref==11))
replace ed_type=2 if (qyear>955 & educ_ref==12)
replace ed_type=3 if (qyear>955 & (educ_ref>=13 & educ_ref<=14))
replace ed_type=4 if (qyear>955 & (educ_ref>=15 & educ_ref<=17))
forvalues x=1/4 {
gen ed`x'=0
replace ed`x'=1 if ed_type==`x'
}
*Gen age indicators 
gen age_grp=.
replace age_grp=1 if (age_ref>=0 & age_ref<=34)
replace age_grp=2 if (age_ref>=35 & age_ref<=49)
replace age_grp=3 if (age_ref>=50 & age_ref<=64)
replace age_grp=4 if (age_ref>=65)
*Gen home owner dummy
gen ownhome=.
replace ownhome=1 if (cutenure>=1 & cutenure<=3)
replace ownhome=0 if (cutenure>=4 & cutenure<=6)
gen region_code=region
replace region_code=5 if (bls_urbn==2) 
*Gen demographic groups 
gen dem_group2=10*ftype+emp_type
gen dem_group3=10*ftype+race_type
gen dem_group4=10*ftype+region_code
gen dem_group6=100*ftype+10*ed_type+race1
*Change dem_group6 into continuous variable 1/40
egen counter=group(dem_group6)
quietly {
forvalues x=1/40 {
gen grp_dum`x'=0
replace grp_dum`x'=1 if counter==`x'
}
}
*Create files for each year's merge
preserve 
keep if (ref_year==1961 | ref_year==1972 | ref_year==1973)
*drop observations that were dropped in predictions 
drop if houseval==99
save data_for_pred_merge60 , replace
restore 

preserve 
keep if (ref_year>=1980 & ref_year<=1984) 
drop if ref_year==1979
save data_for_pred_merge80 , replace 
restore 

*create new housing predictions for 1961 and 1980-81 
do qregressions_new_v2.do 

*Create inequality figures for all years (NOT accounting for the new predicted 
*rent equivalent) This creates an Excel worksheet that has data for 10th, 50th, and 
*90th percentiles as well as 90/10, 50/10, and 90/50 ratios. There is also a 
*formatted Excel worksheet. The first sheet of data in the formatted worksheet
*is simply the data from the output of the following program. To change data,
*copy and past the first sheet from the program output into the first sheet of 
*the formatted version. 
do cons_inequality_data_no_adj_new

*create the same measures for years 1961, 1972-73, and 1980-81 
do cons_inequality_data_predhome_new_v2

*THE FINAL OUTPUT WILL BE FOUR EXCEL FILES: cons_inequality_data_adj.xls; 
*cons_inequality_data_adj60.xls; cons_inequality_data_adj70 AND cons_inequality_data_adj80.xls
*REPLACE THE ROW OF DATA IN cons_inequality_data_adj.xls FOR YEAR 1961 WITH THE
*ROW FROM cons_inequality_data_adj60.xls SIMILARLY, REPLACE THE ROWs FOR 1972, 1973, 1980 AND
*1981 WITH THE ROWS FROM cons_inequality_data_adj70 and cons_inequality_data_adj80.xls, respectively 

*FINALLY, AFTER ROW REPLACEMENT, COPY THE WORKSHEET OF DATA FROM 
*cons_inequality_data_adj.xls AND REPLACE THE FIRST SHEET OF DATA IN 
*cons_inequality_format_w_adj.xls WITH THE NEWLY PRODUCED DATA

*create components worksheet
do component_pctiles_noadj_new 
do income_pctiles_new 

*The final output for the components worksheet will be two files: component_pctiles.xls
*and income_pctiles.xls
*Using the preformatted file component_pctiles_format.xlsx, copy and paste the data
*from component_pctiles.xls to the corresponding tab in the formatted file
*The data from income_pctiles.xls can be copied and pasted to the tab in 
*component_pctiles_format.xlsx titled "income_means"
